//
//  MyOrdersViewController.m
//  jingdongfang
//
//  Created by MTiMac02 on 2017/8/22.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "MyOrdersViewController.h"
#import "SliderViewController.h"
#import "MyOrderViewController.h"
#import "MyBuyViewController.h"
#import "ComplaintViewController.h"

@interface MyOrdersViewController() <SliderViewControllerDelegate> {
    MyOrderViewController *orderVC;
    MyBuyViewController *buyVC;
    UIButton *right;
}
@property (nonatomic, strong)  SliderViewController *sliderVC;

@end

@implementation MyOrdersViewController
@synthesize sliderVC;
#pragma mark -生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.title = @"我的订单";
    self.title = NSLocalizedString(@"My Order", nil);
    self.view.backgroundColor=[UIColor c2Color];
    
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
//    [self ig_setupRightButtonItemWithTitle:@"投诉" image:nil highLightedImage:nil];
    [self ig_setupRightButtonItemWithTitle:NSLocalizedString(@"Complain", nil) image:nil highLightedImage:nil];
    
    sliderVC = [[SliderViewController alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT-NavigationMagin)];
    sliderVC.slideSwitchViewDelegate = self;
  
    sliderVC.shadowImageView.backgroundColor=[UIColor clearColor];
    sliderVC.topScrollView.backgroundColor= [UIColor c2Color];
    sliderVC.tabItemNormalColor = RGB(115, 115, 115);
    sliderVC.tabItemSelectedColor =[UIColor blackColor];
    [self.view addSubview:sliderVC];

    [sliderVC buildUI];
    
    if (self.index)
    {
        [sliderVC selectIndex:self.index];
    }
}

- (void)setIndex:(NSInteger)index {
    if (_index != index) {
        _index = index;
    }
    if (sliderVC) {
        [sliderVC selectIndex:index];
      [self loadVc];
    }
}

- (void)upDataSucc:(NSNotification *)not {
    BOOL isPublic = [not.object boolValue];
    self.index = isPublic ? 0 : 1;
}
-(void)loadVc{
    [orderVC viewDidLoad];
    [buyVC viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [buyVC viewDidLoad];
    [orderVC viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(coverWindowClick) name:@"click" object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"click" object:nil];
}
#pragma mark - SliderViewControllerDelegate

- (NSUInteger)numberOfTab:(SliderViewController *)view {
    return 2;
}

- (UIViewController *)slideSwitchView:(SliderViewController *)view viewOfTab:(NSUInteger)number {
    __weak MyOrdersViewController *weakSelf = self;
    if (number == 0)
    {
        buyVC = [[MyBuyViewController alloc] initWithNibName:@"MyBuyViewController" bundle:nil];
//        buyVC.title = @"我购买的";
        buyVC.title = NSLocalizedString(@"My Purchase", nil);
        [buyVC setEditBlock:^(BOOL isCancel){
            [weakSelf changeRightItemState:isCancel];
        }];
        buyVC.Mybuynav= self.navigationController;
        return buyVC;
    }
    else if (number == 1)
    {
        orderVC = [[MyOrderViewController alloc] initWithNibName:@"MyOrderViewController" bundle:nil];
//       orderVC.title = @"我出售的";
        orderVC.title = NSLocalizedString(@"My Selling", nil);
        [orderVC setEditBlock:^(BOOL isCancel){
            [weakSelf changeRightItemState:isCancel];
        }];
        orderVC.Myordernav = self.navigationController;
        return orderVC;
    }
    return nil;
}


- (void)changeRightItemState:(BOOL)cancle
{
    if (cancle)
    {
        [right setSelected:YES];
        self.sliderVC.rootScrollView.scrollEnabled = NO;
        self.sliderVC.topScrollView.userInteractionEnabled = NO;
    }
//    else
//    {
//        [self  canel:nil];
//    }
}

- (void)ig_rightButtonPressed:(id)sender {
    [UIViewController ig_pushViewControllerClass:[ComplaintViewController class] animated:YES];
}

- (void)coverWindowClick
{
    
    if (sliderVC.userSelectedChannelID == 100)
    {
        [buyVC scrollToTOP];
    }
    else if (sliderVC.userSelectedChannelID == 101)
    {
        [orderVC scrollToTOP];
    }
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
